package org.itboys.mobile.service.mysql.web.product;


import org.itboys.commons.utils.tools.ListUtil;
import org.itboys.commons.utils.tools.LongUtil;
import org.itboys.mobile.dao.product.SundayProductCategorySecondStandardMapper;
import org.itboys.mobile.dao.product.SundayProductStandardMapper;
import org.itboys.mobile.entity.mysql.product.SundayProductCategorySecondStandard;
import org.itboys.mobile.entity.mysql.product.SundayProductStandard;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 作者：jiangxiong
 * 日期：2016年9月13日
 * 联系方式 ：empty90911@126.com
 * 描述：圣代_商品_工具（多规格）_service
 * 
 */
@Service
public class SundayProductUtilService {
	@Autowired
	private SundayProductCategorySecondStandardMapper secondMapper;
	@Autowired
	private SundayProductStandardMapper standardMapper;
	/*@Autowired
	private HaoYiProductServiceWeb productService;*/
	/**
	 * 组合商品需要的规格。
	 * 参数格式。"1,2,3,", "4,5,6"。参数最多大小4组
	 */
	public Map<String,Object> packageProductStandarads(List<String> idsStrList,Long productId){
		if(idsStrList.size()==1){
			return packageProductStandarads1(idsStrList,productId);
		}else if(idsStrList.size()==2){
			return packageProductStandarads2(idsStrList,productId);
		}else if(idsStrList.size()==3){
			return packageProductStandarads3(idsStrList,productId);
		}else if(idsStrList.size()==4){
			return packageProductStandarads4(idsStrList,productId);
		}
		
		return null;
	}
	
	//针对一组规格
	public Map<String,Object> packageProductStandarads1(List<String> idsStrList,Long productId){
		System.out.println("开始组装1");
		Map<String,Object> result = new HashMap<String, Object>();
		//Map<String,Object> tableHead = new HashMap<String, Object>();
		//表头
		List<String> tableHead = new ArrayList<String>();
		//表数据
		List<Map<String,Object>> tableDatas = new ArrayList<Map<String,Object>>();
		//参数
		Map<String,Object> param = new HashMap<String, Object>();
		//商品已有规格
		
		List<SundayProductStandard> productStandards = new ArrayList<SundayProductStandard>();
		if(LongUtil.isNotZreo(productId)){
			param.clear();
			param.put("productId", productId);
			productStandards=standardMapper.select(param);
		}
		
		
		List<List<Long>> secondIdsList = stringChangeToLong(idsStrList);
		
		
		System.out.println("共有"+secondIdsList.size()+"组数据");
		if(ListUtil.isNull(idsStrList))return result;
		//1,如果只有一组规格
		//if(secondIdsList.size()>1){
			List<Long> secondIds1=secondIdsList.get(0);
			param.put("ids", secondIds1);
			List<SundayProductCategorySecondStandard> seconds1=secondMapper.select(param);
			String firstName1=seconds1.get(0).getFirstName();
			tableHead.add(firstName1);
			
			for(SundayProductCategorySecondStandard second1:seconds1){
				Map<String,Object> tableData1 = new HashMap<String, Object>();
				tableData1.put("second1", second1);
				//价格和库存
				tableData1.put("price", 0);
				tableData1.put("store", 0);
				//2017年12月15日，玖琪自定义

				tableData1.put("sku", "");
				tableData1.put("wheelSize", "");
				tableData1.put("length", "");
				tableData1.put("wide", "");
				tableData1.put("height", "");
				tableData1.put("height", "");
				tableData1.put("weight", "");
				tableData1.put("id", 0l);
				if(ListUtil.isNotNull(productStandards)){
					for(SundayProductStandard s:productStandards){
						String secondId=second1.getId().toString();
						if(s.getSecondId().equals(secondId)){
							/*tableData1.put("price", s.getPrice());
							tableData1.put("store", s.getStore());*/
							//2017年12月15日，玖琪自定义
							tableData1.put("sku", s.getSku());
							tableData1.put("wheelSize", s.getWheelSize());
							tableData1.put("length", s.getLength());
							tableData1.put("wide",s.getWide());
							tableData1.put("height", s.getHeight());
							tableData1.put("weight", s.getWeight());
							//已经存在的商品规格ID
							tableData1.put("id", s.getId());
							break;
						}
						
					}
				}
				tableDatas.add(tableData1);
			}
		//}
		
		/*tableHead.add("价格");
		tableHead.add("库存");*/
		result.put("tableHead", tableHead);
		result.put("tableDatas", tableDatas);
		return result;
	}
	//针对两组组规格
	public Map<String,Object> packageProductStandarads2(List<String> idsStrList,Long productId){
		System.out.println("开始组装2");
		Map<String,Object> result = new HashMap<String, Object>();
		//表头
		List<String> tableHead = new ArrayList<String>();
		//表数据
		List<Map<String,Object>> tableDatas = new ArrayList<Map<String,Object>>();
		//参数
		Map<String,Object> param = new HashMap<String, Object>();
		//转换
		List<List<Long>> secondIdsList = stringChangeToLong(idsStrList);
		//商品已有规格
		
		List<SundayProductStandard> productStandards = new ArrayList<SundayProductStandard>();
		if(LongUtil.isNotZreo(productId)){
			param.clear();
			param.put("productId", productId);
			productStandards=standardMapper.select(param);
		}
		//System.out.println("共有"+secondIdsList.size()+"组数据");
		if(ListUtil.isNull(idsStrList))return result;
		//1,如果只有一组规格
		//if(secondIdsList.size()==2){
			param.clear();
			List<Long> secondIds1=secondIdsList.get(0);
			param.put("ids", secondIds1);
			List<SundayProductCategorySecondStandard> seconds1=secondMapper.select(param);
			String firstName1=seconds1.get(0).getFirstName();
			tableHead.add(firstName1);
			
			
			param.clear();
			List<Long> secondIds2=secondIdsList.get(1);
			param.put("ids", secondIds2);
			List<SundayProductCategorySecondStandard> seconds2=secondMapper.select(param);
			String firstName2=seconds2.get(0).getFirstName();
			tableHead.add(firstName2);
			
			for(SundayProductCategorySecondStandard second1:seconds1){
					for(SundayProductCategorySecondStandard second2:seconds2){
						
						Map<String,Object> tableData2 = new HashMap<String, Object>();
						tableData2.put("second1", second1);
						tableData2.put("second2", second2);
						//价格和库存。
						tableData2.put("sku", "");
						tableData2.put("wheelSize", "");
						tableData2.put("length", "");
						tableData2.put("wide", "");
						tableData2.put("height", "");
						tableData2.put("height", "");
						tableData2.put("weight", "");
						/*tableData2.put("price", 0);
						tableData2.put("store", 0);*/
						tableData2.put("id", 0l);
						//显示已有的价格和库存
						if(ListUtil.isNotNull(productStandards)){
							for(SundayProductStandard s:productStandards){
								String secondId=second1.getId()+"-"+second2.getId();
								if(s.getSecondId().equals(secondId)){
									/*tableData2.put("price", s.getPrice());
									tableData2.put("store", s.getStore());*/
									//2017年12月15日，玖琪自定义
									tableData2.put("sku", s.getSku());
									tableData2.put("wheelSize", s.getWheelSize());
									tableData2.put("length", s.getLength());
									tableData2.put("wide",s.getWide());
									tableData2.put("height", s.getHeight());
									tableData2.put("weight", s.getWeight());
									tableData2.put("id", s.getId());
									break;
								}
								
							}
						}

						tableDatas.add(tableData2);
						
					}
			//}
		}

	/*	tableHead.add("价格");
		tableHead.add("库存");*/
		//2017年12月15日
		tableHead.add("SKU");
		tableHead.add("轮胎尺寸");
		tableHead.add("长度");
		tableHead.add("宽度");
		tableHead.add("高度");
		tableHead.add("重量");
	//2017年12月15日，
		result.put("tableHead", tableHead);
		for(String head:tableHead){
			System.err.println("表头"+head);
		}
		System.out.println("表头"+tableHead.size()+"大小");
		System.out.println("共组装了"+tableDatas.size()+"数据");
		result.put("tableDatas", tableDatas);
		return result;
	}
	//针对三组组规格
	public Map<String,Object> packageProductStandarads3(List<String> idsStrList,Long productId){
		System.out.println("开始组装3");
		Map<String,Object> result = new HashMap<String, Object>();
		//表头
		List<String> tableHead = new ArrayList<String>();
		//表数据
		List<Map<String,Object>> tableDatas = new ArrayList<Map<String,Object>>();
		//参数
		Map<String,Object> param = new HashMap<String, Object>();
		//转换
		List<List<Long>> secondIdsList = stringChangeToLong(idsStrList);
		//商品已有规格
		
		List<SundayProductStandard> productStandards = new ArrayList<SundayProductStandard>();
		if(LongUtil.isNotZreo(productId)){
			param.clear();
			param.put("productId", productId);
			productStandards=standardMapper.select(param);
		}
		//System.out.println("共有"+secondIdsList.size()+"组数据");
		if(ListUtil.isNull(idsStrList))return result;

			param.clear();
			List<Long> secondIds1=secondIdsList.get(0);
			param.put("ids", secondIds1);
			List<SundayProductCategorySecondStandard> seconds1=secondMapper.select(param);
			String firstName1=seconds1.get(0).getFirstName();
			tableHead.add(firstName1);
			
			
			param.clear();
			List<Long> secondIds2=secondIdsList.get(1);
			param.put("ids", secondIds2);
			List<SundayProductCategorySecondStandard> seconds2=secondMapper.select(param);
			String firstName2=seconds2.get(0).getFirstName();
			tableHead.add(firstName2);
			
			param.clear();
			List<Long> secondIds3=secondIdsList.get(2);
			param.put("ids", secondIds3);
			List<SundayProductCategorySecondStandard> seconds3=secondMapper.select(param);
			String firstName3=seconds3.get(0).getFirstName();
			tableHead.add(firstName3);
			
			for(SundayProductCategorySecondStandard second1:seconds1){
				for(SundayProductCategorySecondStandard second2:seconds2){
					for(SundayProductCategorySecondStandard second3:seconds3){
						Map<String,Object> tableData3 = new HashMap<String, Object>();
						tableData3.put("second1", second1);
						tableData3.put("second2", second2);
						tableData3.put("second3", second3);
						//价格和库存。
						
						tableData3.put("price", 0);
						tableData3.put("store", 0);
						tableData3.put("id", 0l);
						//显示已有的价格和库存
						if(ListUtil.isNotNull(productStandards)){
							for(SundayProductStandard s:productStandards){
								//组合起来的二级规格id1是否等于商品规格的中的规格ID。
								String secondId=second1.getId()+"-"+second2.getId()+"-"+second3.getId();
								if(s.getSecondId().equals(secondId)){
									/*tableData3.put("price", s.getPrice());
									tableData3.put("store", s.getStore());*/
									tableData3.put("id", s.getId());
									break;
								}
								
							}
						}
						tableDatas.add(tableData3);
					}
				}
		}
			
	/*	tableHead.add("价格");
		tableHead.add("库存");*/
		result.put("tableHead", tableHead);
		//for(String head:tableHead){
		////	System.err.println("表头"+tableHead);
		//}
		//System.out.println("共组装了"+tableDatas.size()+"数据");
		result.put("tableDatas", tableDatas);
		return result;
	}
	//针对四组组规格
	public Map<String,Object> packageProductStandarads4(List<String> idsStrList,Long productId){
		System.out.println("开始组装4");
		Map<String,Object> result = new HashMap<String, Object>();
		//表头
		List<String> tableHead = new ArrayList<String>();
		//表数据
		List<Map<String,Object>> tableDatas = new ArrayList<Map<String,Object>>();
		//参数
		Map<String,Object> param = new HashMap<String, Object>();
		//转换
		List<List<Long>> secondIdsList = stringChangeToLong(idsStrList);
		//商品已有规格
		List<SundayProductStandard> productStandards = new ArrayList<SundayProductStandard>();
		if(LongUtil.isNotZreo(productId)){
			param.clear();
			param.put("productId", productId);
			productStandards=standardMapper.select(param);
		}
	
		if(ListUtil.isNull(idsStrList))return result;
			param.clear();
			List<Long> secondIds1=secondIdsList.get(0);
			param.put("ids", secondIds1);
			List<SundayProductCategorySecondStandard> seconds1=secondMapper.select(param);
			String firstName1=seconds1.get(0).getFirstName();
			tableHead.add(firstName1);
			
			
			param.clear();
			List<Long> secondIds2=secondIdsList.get(1);
			param.put("ids", secondIds2);
			List<SundayProductCategorySecondStandard> seconds2=secondMapper.select(param);
			String firstName2=seconds2.get(0).getFirstName();
			tableHead.add(firstName2);
			
			param.clear();
			List<Long> secondIds3=secondIdsList.get(2);
			param.put("ids", secondIds3);
			List<SundayProductCategorySecondStandard> seconds3=secondMapper.select(param);
			String firstName3=seconds3.get(0).getFirstName();
			tableHead.add(firstName3);
			
			param.clear();
			List<Long> secondIds4=secondIdsList.get(3);
			param.put("ids", secondIds4);
			List<SundayProductCategorySecondStandard> seconds4=secondMapper.select(param);
			String firstName4=seconds4.get(0).getFirstName();
			tableHead.add(firstName4);
			
			for(SundayProductCategorySecondStandard second1:seconds1){
				for(SundayProductCategorySecondStandard second2:seconds2){
					for(SundayProductCategorySecondStandard second3:seconds3){
						for(SundayProductCategorySecondStandard second4:seconds4){
							Map<String,Object> tableData4 = new HashMap<String, Object>();
							tableData4.put("second1", second1);
							tableData4.put("second2", second2);
							tableData4.put("second3", second3);
							tableData4.put("second4", second4);
							//价格和库存。
							
							tableData4.put("price", 0);
							tableData4.put("store", 0);
							tableData4.put("id",0l);
							//显示已有的价格和库存
							if(ListUtil.isNotNull(productStandards)){
								for(SundayProductStandard s:productStandards){
									//组合起来的二级规格id1是否等于商品规格的中的规格ID。
									String secondId=second1.getId()+"-"+second2.getId()+"-"+second3.getId()+"-"+second4.getId();
									if(s.getSecondId().equals(secondId)){
										/*tableData4.put("price", s.getPrice());
										tableData4.put("store", s.getStore());*/
										tableData4.put("id", s.getId());
										break;
									}
									
								}
							}
							tableDatas.add(tableData4);
						}
					}
				}
		}
		
		/*tableHead.add("价格");
		tableHead.add("库存");*/
		result.put("tableHead", tableHead);
		result.put("tableDatas", tableDatas);
		return result;
	}
	
	//获取商品已有的分类二级规格ID
	public List<Long> getSelectedSecond(Long productId){
		List<Long> result = new ArrayList<Long>();
		Map<String,Object> param = new HashMap<String, Object>();
		param.put("productId", productId);
		List<SundayProductStandard> standards =standardMapper.select(param);
		for(SundayProductStandard standard:standards){
			for(String secondId:standard.getSecondId().split("-")){
				result.add(Long.valueOf(secondId));
			}
			
		}
		return result;
	}
	
	
	
	//转换工具类
	public List<List<Long>> stringChangeToLong(List<String> idsStrList){
		List<List<Long>> idsList = new ArrayList<List<Long>>();
		
		for(String idsStr:idsStrList){
			
			List<Long> ids = new ArrayList<Long>();
			for(String idStr:idsStr.split("xiong")){
				ids.add(Long.valueOf(idStr));
			}
			idsList.add(ids);
		}
		return idsList;
	}
}
